import java.sql.*;
import java.util.*;

public class Main {
	Connection connection = null;
	String url = "";
	String[] path={"user_login","data"},
			params={"name","password"},
			values;

	public static void main(String[] args) {
		Main mainOBJ;
		Scanner scanner;
		Map<String ,String> urlParams=new TreeMap<>();
		urlParams.put("serverTimeZone","GMT");
		urlParams.put("useSSL",String.valueOf(false));
		String url=SQLformatter.sqlUrl(3306,"user_login",urlParams);
		try {
			System.out.println(url);
			System.out.println("logging in...");
			mainOBJ = new Main();
			mainOBJ.connection = DriverManager.getConnection
					(url,"root", "A7btngqxA7btngqx");
		} catch (Exception e) {
			if (e instanceof SQLException) {
				System.out.println("SQLException");
			}
			System.out.println(e.getMessage());
			return;
		}
		System.out.println("open successful");
		scanner=new Scanner(System.in);
		String[] choices={"1.add","2.check","3.update","4,modify","0.exit"};
		while (true){
			try {
				mainOBJ.check();
				String name= scanner.nextLine(),
						password= scanner.nextLine();
				if(name==null||name.isEmpty())break;
				mainOBJ.add(name,password);
			}catch (SQLException e){
				System.out.println(e.getMessage());
				break;
			}
		}
	}
	Main() {}
	void add(String name, String password) throws SQLException {
		String content;

		values=new String[2];
		values[0]=name;
		values[1]=password;

		content="INSERT INTO "
				+SQLformatter.path_surround(path)
				+SQLformatter.titleAppend(params)
				+"VALUES"+SQLformatter.parentheses(
						SQLformatter.value_quote(values)
		)+";";
		System.out.println(content);
		PreparedStatement statement= connection.prepareStatement(content);
		statement.executeUpdate();
		System.out.println("name:"+name+"\tpassword:"+password+"\t--appended");
	}
	void check() throws SQLException {
		int colNum=1;
		ResultSet resultSet=connection.
				prepareStatement("SELECT * FROM data ")
				.executeQuery();
		while (resultSet.next()){
			for(colNum=1;colNum<=2;colNum++)
				System.out.print(resultSet.getString(colNum)+"\t");
			System.out.println();
		}
	}
}
